import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { NzModalRef, NzModalService } from 'ng-zorro-antd';
import {RollService} from "../my-service/roll.service";

import { UtilService } from '../my-service/util.service';

import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';


@Component({
  selector: 'nav-root',
  templateUrl: './nav.component.html',
  styleUrls: ['./nav.component.css']
})
export class NavComponent implements OnInit{

  activeMenu: string = 'home';
  defaultCover = '/assets/img/rollIndex/touxiang.png';
  readImgUrl:string = null;
  userGuid:string = null;
  user = {
    Cover:''
  };
  menuList = [
    {name: '我的', address: 'home'},
    {name: '内容商店', address: 'contentStore'},
    {name: '应用商店', address: 'appStore'},
    {name: '发现', address: 'find'}
  ];
  constructor(
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private modal: NzModalService,
    private utilService: UtilService,
    private rollService:RollService
  ) {
    this.readImgUrl =this.utilService.readImgUrl;
  }
 getUserInfo() {
    this.rollService.getUserInfo(this.userGuid).then(res=>{
      if(res.status == 1){
        this.user = res.data;
      }else {
        console.log(res);
      }
    })
 }
 getUserGuid() {
    this.utilService.getUserInfo().then(res=>{
      if(res.status==1){
        this.userGuid = res.userGuid;
        this.getUserInfo();
      }
    })
 }
  ngOnInit() {
    this.getUserGuid();
    this.router.events
    .filter(event => event instanceof NavigationEnd)
    .map(() => this.activatedRoute)
    .map(route => {
      while (route.firstChild) route = route.firstChild;
      return route;
    })
    .filter(route => route.outlet === 'primary')
    .mergeMap(route => route.data)
  .subscribe(event => {
      this.activeMenu = event.menu;
    });
  }

  changeRouting(address) {
    this.router.navigate([address]);
  }

  goHome() {
    this.router.navigate(['/']);
  }
  goto(path){
    this.router.navigate([`${path}`]);
  }
  // 测试/示例方法，可删除
  getUserInfoTest() {
    this.utilService.getUserInfo().then( res => {
      this.userGuid = res.userGuid;
    });
  }

  logout() {
    this.utilService.comfirm('确认退出登录？', () => {
      this.utilService.logout().then( res => {
        if(res.status === 1) {
          this.utilService.remind('success', '退出成功！');
          window.location.reload();
        } else this.utilService.remind('error', '退出失败，请重试！');
      });
    });
  }

}
